package com.crm.system.service.impl.cluePoolManager;

import com.alibaba.fastjson.JSONObject;
import com.crm.common.model.Enum.CluePoolAllocationEnum;
import com.crm.common.model.Enum.CluePoolRuleTypeEnum;
import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.common.util.CommonUtils;
import com.crm.model.entity.cluePoolManager.CluePoolRule;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.qo.cluePoolManager.CluePoolPublicQO;
import com.crm.model.qo.cluePoolManager.CluePoolRuleQO;
import com.crm.model.vo.cluePoolManager.CluePoolDistributionVO;
import com.crm.model.vo.cluePoolManager.CluePoolRuleVO;
import com.crm.service.cluePoolManager.CluePoolRuleDbService;
import com.crm.system.service.cluePoolManager.CluePoolDistributionService;
import com.crm.system.service.cluePoolManager.CluePoolRuleService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author luojie
 * @date 2020/12/25 15:20
 */
@Service
public class CluePoolRuleServiceImpl implements CluePoolRuleService {

    @Autowired
    private CluePoolRuleDbService cluePoolRuleDbService;
    @Autowired
    private CluePoolDistributionService cluePoolDistributionService;

    @Override
    public Boolean addCluePoolRule(CluePoolPublicQO cluePoolPublicQO,String cluePoolId) {
       if(cluePoolPublicQO.getAllocation()== CluePoolAllocationEnum.PASSIVE_DISTRIBUTION.getCluePoolAllocationCode()) {
           //被动分配
           //配置分配规则
           insertCluePoolRule(cluePoolId,CluePoolRuleTypeEnum.DISTRIBUTION.getCluePoolRuleTypeCode(),
                   cluePoolPublicQO.getAssignedQuantity(), cluePoolPublicQO.getAllocationTimeInterval(),null);
           //添加被动分配人员名单表
           cluePoolDistributionService.addCluePoolDistribution(cluePoolPublicQO.getAssignedPersonList(),cluePoolId);
       }else if(cluePoolPublicQO.getAllocation()== CluePoolAllocationEnum.MIXED_MODE.getCluePoolAllocationCode()){
           //混合模式
           //配置领取规则
           insertCluePoolRule(cluePoolId,CluePoolRuleTypeEnum.RECEIVE.getCluePoolRuleTypeCode(),
                   cluePoolPublicQO.getMaximumNumber(), cluePoolPublicQO.getCollectionTimeInterval(),cluePoolPublicQO.getContinueTimeInterval());
       }

        //回退时间间隔
        insertCluePoolRule(cluePoolId, CluePoolRuleTypeEnum.RETURN.getCluePoolRuleTypeCode(),
                null,cluePoolPublicQO.getReturnInterval(),null);
        return true;
    }

    public void insertCluePoolRule(String cluePoolId,Integer type,Integer number,Integer timeInterval,Integer continueTimeInterval){
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        CluePoolRule cluePoolRule = new CluePoolRule();
        cluePoolRule.setCluePoolRuleId(CommonUtils.getGUID());
        cluePoolRule.setRuleType(type);
        cluePoolRule.setTimeInterval(timeInterval);
        cluePoolRule.setContinueTimeInterval(continueTimeInterval);
        cluePoolRule.setCluePoolNumber(number);
        cluePoolRule.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        cluePoolRule.setCreatePeopleId(userAccount.getAccountId());
        cluePoolRule.setCreateTm(new Date());
        cluePoolRule.setCluePoolId(cluePoolId);
        cluePoolRule.setUpdatePeopleId(userAccount.getAccountId());
        cluePoolRule.setUpdateTm(new Date());
        cluePoolRuleDbService.insertCluePoolRule(cluePoolRule);
    }

    @Override
    public CluePoolRuleVO selectCluePoolRuleByCluePoolId(String cluePoolId) {
        List<JSONObject> assignedPersonList = new ArrayList<JSONObject>();
        CluePoolRuleVO cluePoolRuleVO = new CluePoolRuleVO();
        List<CluePoolRule> cluePoolRules = cluePoolRuleDbService.selectCluePoolRuleByCluePoolId(cluePoolId);
        if(!cluePoolRules.isEmpty()){
            for (CluePoolRule cluePoolRule : cluePoolRules) {
                //分配方式：1-领取；2-回退；3-分配
                if(cluePoolRule.getRuleType() == CluePoolRuleTypeEnum.RECEIVE.getCluePoolRuleTypeCode()){
                    cluePoolRuleVO.setMaximumNumber(String.valueOf(cluePoolRule.getCluePoolNumber()));
                    cluePoolRuleVO.setCollectionTimeInterval(String.valueOf(cluePoolRule.getTimeInterval()));
                    cluePoolRuleVO.setContinueTimeInterval(String.valueOf(cluePoolRule.getContinueTimeInterval()));
                }else if(cluePoolRule.getRuleType() == CluePoolRuleTypeEnum.RETURN.getCluePoolRuleTypeCode()){
                    cluePoolRuleVO.setReturnInterval(String.valueOf(cluePoolRule.getTimeInterval()));
                }else if(cluePoolRule.getRuleType() == CluePoolRuleTypeEnum.DISTRIBUTION.getCluePoolRuleTypeCode()){
                    cluePoolRuleVO.setAssignedQuantity(String.valueOf(cluePoolRule.getCluePoolNumber()));
                    cluePoolRuleVO.setAllocationTimeInterval(String.valueOf(cluePoolRule.getTimeInterval()));
                }
            }
            List<CluePoolDistributionVO> cluePoolDistributionVOS
                    = cluePoolDistributionService.selectCluePoolDistributionVOList(cluePoolId,
                    Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
            for (CluePoolDistributionVO cluePoolDistributionVO : cluePoolDistributionVOS) {
                JSONObject jsonObject = new JSONObject();
                String companyId = cluePoolDistributionVO.getCompanyId();
                String companyName = cluePoolDistributionVO.getCompanyName();
                String peopleId = cluePoolDistributionVO.getPeopleId();
                String userName = cluePoolDistributionVO.getUserName();
                if(!StringUtils.isBlank(companyId) && !StringUtils.isBlank(companyName)
                        && !StringUtils.isBlank(peopleId) && !StringUtils.isBlank(userName)){
                    jsonObject.put("companyId", companyId);
                    jsonObject.put("companyName", companyName);
                    jsonObject.put("peopleId", peopleId);
                    jsonObject.put("approverName", userName);
                }
                if(!jsonObject.isEmpty()){
                    assignedPersonList.add(jsonObject);
                }
            }
            cluePoolRuleVO.setAssignedPersonList(assignedPersonList);
        }
        return cluePoolRuleVO;
    }

    @Override
    public Boolean updateCluePoolRule(CluePoolRuleQO cluePoolRuleQO, String cluePoolId) {
        Integer allocation = Integer.parseInt(cluePoolRuleQO.getAllocation());
        String assignedQuantity = cluePoolRuleQO.getAssignedQuantity();
        String allocationTimeInterval = cluePoolRuleQO.getAllocationTimeInterval();
        String maximumNumber = cluePoolRuleQO.getMaximumNumber();
        String collectionTimeInterval = cluePoolRuleQO.getCollectionTimeInterval();
        String returnInterval = cluePoolRuleQO.getReturnInterval();
        String continueTimeInterval = cluePoolRuleQO.getContinueTimeInterval();
        if(allocation == CluePoolAllocationEnum.                     PASSIVE_DISTRIBUTION.getCluePoolAllocationCode()) {
            //被动分配
            //配置分配规则
            insertCluePoolRule(cluePoolId,CluePoolRuleTypeEnum.DISTRIBUTION.getCluePoolRuleTypeCode(),
                    !StringUtils.isBlank(assignedQuantity) ? Integer.parseInt(assignedQuantity) : null,
                    !StringUtils.isBlank(allocationTimeInterval) ? Integer.parseInt(allocationTimeInterval) : null ,null);
            //添加被动分配人员名单表
            cluePoolDistributionService.addCluePoolDistribution(cluePoolRuleQO.getAssignedPersonList(),cluePoolId);
        }else if(allocation == CluePoolAllocationEnum.MIXED_MODE.getCluePoolAllocationCode()){
            //混合模式
            //配置领取规则
            insertCluePoolRule(cluePoolId,CluePoolRuleTypeEnum.RECEIVE.getCluePoolRuleTypeCode(),
                    !StringUtils.isBlank(maximumNumber) ? Integer.parseInt(maximumNumber) : null,
                    !StringUtils.isBlank(collectionTimeInterval) ? Integer.parseInt(collectionTimeInterval) : null,
                    !StringUtils.isBlank(continueTimeInterval) ? Integer.parseInt(continueTimeInterval) : null);
        }

        //回退时间间隔
        insertCluePoolRule(cluePoolId, CluePoolRuleTypeEnum.RETURN.getCluePoolRuleTypeCode(),
                null, !StringUtils.isBlank(returnInterval) ? Integer.parseInt(returnInterval) : null,null);
        return true;
    }

    @Override
    public Boolean deleteCluePoolRuleByCluePoolId(String cluePoolId) {
        return cluePoolRuleDbService.deleteCluePoolRuleByCluePoolId(cluePoolId);
    }

}
